package com.en.effectiveJava.chapter2.finalizer7;

/**
 * 终结方法测试
 * Create by EN
 * On 6/06/18 1:17 PM
*/
public class FinalizerTest extends Fuu {

    @Override
    protected void finalize() throws Throwable {
        System.out.println("FinalizerTest is finalized!");
        //super.finalize();
    }

    public static void main(String[] args) throws InterruptedException {
        FinalizerTest test = new FinalizerTest();
        test = null;
        //终结方法不一定会执行
        System.gc();
        //Thread.sleep(1000);
    }

}

class Fuu {
    //保证子类被垃圾回收后，父类的法终结方法也能及时的执行
    private final Object finalizerGuardian = new Object(){
        @Override
        protected void finalize() throws Throwable {
            System.out.println("Fuu is finalized!");
            super.finalize();
        }
    };
}